<?php

/**
 * Well Framework
 * 
 * Abstract View class of MVC pattern
 * Uses in command views & layouts helpers views
 * 
 * @package     Well
 * 
 * @author Valeriy Lazarevych <valeriy.fremar@gmail>
 * @copyright Fremar ltd.
 */

class View
{
    /**
     * View file name
     * 
     * @var type string
     */
    protected $view_name;
    
    /**
     * View file suffix
     * 
     * @var type string
     */
    protected $view_suffix  =   '.phtml';
    
    /**
     * Path to views placeholder
     * 
     * It can be on commands views ||
     * layout helper views
     * 
     * @todo move views path to common config file
     * 
     * @var type string
     */
    protected $views_path   =   'views/scripts/';  
    
    /**
     * Holder of action container sending to own view
     * 
     * @var type array
     */
    protected $container   =   array();
    
    /**
     * Contain application Request
     * to get access to app request by view source
     * 
     * @var Request $request 
     */
    protected $request     =    null;

    
    
    /**
     * Put $view_name to $this->viewkey
     * 
     * @param string $view_name
     */
    public function __construct($view_name)
    {  
        // Initialize view name
        $this-> view_name   =   $view_name;
        
        // Get app request
        $this-> request     =   ApplicationRegistry::get('request');
    }
        
    /**
     * Render view file
     * 
     * @return type phtml of view file
     */
    public function render()
    {                
        // Get view file name
        $file_name  =   $this->view_name . $this->view_suffix;        
        
        // Include view source or faleir addFeedback
        if ( file_exists( $this->views_path . $file_name ) ){                                   
                  include $this->views_path . $file_name;                                                                    
        } else {
                  $this->request->addFeedback( 'Can not find view file '.$this->views_path . $file_name );  }
                
        return null;
    }        
    
    /**
     * Return view name
     * 
     * @return type string
     */
    public function getView()
    {
        return  $this -> view_name;
    }
    
    /**
     * Set View name
     * 
     * @param string $view_name
     */
    public function setView($view_name)
    {                 
        if ( file_exists( $this-> views_path . $view_name . $this->view_suffix ) ){
                          $this-> view_name  = $view_name;            
        } else {
            $this->request->addFeedback('View with name '.$view_name.' can not be put it, because own file has not been found');    }
    }
    
    /**
     * Get view dir path
     * 
     * @return string
     */
    public function getViewsPath()
    {
        return $this->views_path;
    }
    
    /**
     * Set path of view dir
     * Where will find view script
     * 
     * @param string $path 
     */
    public function setViewsPath($path)
    {                        
        if ( is_dir($path) ){
            
              $this-> views_path   =   $path;
        } else {
              $this-> request->addFeedback('View path '.$path.' does not exists');    }
    }
    
    /**
     * Create unexisting property in view
     * Use it to pass on data from action its view
     * 
     * @param string $key
     * @param string $value 
     */
    public function __set($key, $value)
    {
        $this->container[(string)$key]    =   $value;
    }
    
    /**
     * Get view variable
     * 
     * @param string $key
     * @return empty array || string property with $key
     */
    public function __get($key)
    {
        if ( isset($this->container[$key]) ){            
            return $this->container[$key];   
        }
        
        return null;
    }
    
    /**
     * Is a view variable set?
     * 
     * @param string $key
     * @return bool
     */
    public function __isset($key)
    {        
        return (isset($this->container[$key]));
    }
    
    /**
     * Unset a view variable name
     * 
     * @param string $key variable name
     */
    public function __unset($key)
    {
        if ( isset($this->container[$key]) ) {            
             unset($this->container[$key]);
        }
    }
        
}
?>
